package com.biz.primus.ms.promotion.dao.ro;

import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.annotation.RoSortedSet;
import com.biz.primus.model.promotion.enums.PromotionStatusEnum;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author david-liu
 */
@Getter
@Setter
@Ro(key = "pmt:presell")
@RoSortedSet(key = "list", score = "createTimestamp")
public class PresellPromotionRo extends BasePromotionRo<Long> {
    private static final long serialVersionUID = 5738793946935097190L;

    /**
     * 开始时间
     */
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    private LocalDateTime endTime;

    /**
     * 活动名称
     */
    private String name;

    /**
     * 活动描述
     */
    private String description;

    /**
     * 活动生效渠道
     */
    private String channels;

    /**
     * 享受活动的会员等级集合
     */
    private String effectMemberLevels;

    /**
     * 享受活动的会员标签
     */
    private String effectMemberTags;

    /**
     * 启用状态
     */
    private String commonStatus;

    /**
     * 是否能使用优惠券
     */
    private Boolean allowCoupon = false;

    /**
     * 用户是否只能参与一次
     */
    private Boolean userOnceGainLimit = false;

    /**
     * 发货时间
     */
    private LocalDateTime deliverTime;

    /**
     * 判断秒杀活动是否已经完成
     */
    public boolean isPromotionPublished() {
        LocalDateTime now = LocalDateTime.now();
        return now.isBefore(endTime) && now.isAfter(startTime);
    }

    /**
     * 获取秒杀活动的状态
     */
    public PromotionStatusEnum getPromotionStatus(Boolean hasMemberGained) {
        LocalDateTime now = LocalDateTime.now();
        if (now.isBefore(startTime)) {
            return PromotionStatusEnum.NOT_START;
        } else {
            if (now.isAfter(endTime)) {
                return PromotionStatusEnum.FINISHED;
            } else {
                return PromotionStatusEnum.HAS_STARTED;
            }
        }
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }
}
